package com.ehotting.edsta.circuitDesign.controller;

import com.ehotting.edsta.circuitDesign.domain.EsRuleScript;
import com.ehotting.edsta.circuitDesign.service.IEsRuleScriptService;
import com.ehotting.eaf.core.utils.poi.ExcelUtil;
import com.ehotting.eaf.core.web.controller.BaseController;
import com.ehotting.eaf.core.web.domain.AjaxResult;
import com.ehotting.eaf.core.web.page.TableDataInfo;
import com.ehotting.edsta.common.log.annotation.Log;
import com.ehotting.edsta.common.log.enums.BusinessType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


/**
 * 脚本信息Controller
 * 
 * @author edsta
 * @date 2023-06-03
 */
@RestController
@RequestMapping("/script")
public class EsRuleScriptController extends BaseController
{
    @Autowired
    private IEsRuleScriptService esScriptService;

    /**
     * 查询脚本信息列表
     */
//    @PreAuthorize(hasPermi = "edsta:generation:list")
    @GetMapping("/list")
    public TableDataInfo list(EsRuleScript esRuleScript)
    {
        startPage();
        List<EsRuleScript> list = esScriptService.selectEsRuleScriptList(esRuleScript);
        return getDataTable(list);
    }

    @GetMapping("/allList")
    public AjaxResult allList(EsRuleScript esRuleScript)
    {

        List<EsRuleScript> list = esScriptService.selectEsRuleScriptList(esRuleScript);
        return AjaxResult.success(list);
    }

    /**
     * 导出脚本信息列表
     */
//    @PreAuthorize(hasPermi = "edsta:generation:export")
    @Log(title = "脚本信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EsRuleScript esRuleScript) throws IOException
    {
        List<EsRuleScript> list = esScriptService.selectEsRuleScriptList(esRuleScript);
        ExcelUtil<EsRuleScript> util = new ExcelUtil<EsRuleScript>(EsRuleScript.class);
        util.exportExcel(response, list, "脚本信息数据");
    }

    /**
     * 获取脚本信息详细信息
     */
//    @PreAuthorize(hasPermi = "edsta:generation:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(esScriptService.selectEsRuleScriptById(id));
    }

    /**
     * 新增脚本信息
     */
//    @PreAuthorize(hasPermi = "edsta:generation:add")
    @Log(title = "脚本信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EsRuleScript esRuleScript)
    {
        return toAjax(esScriptService.insertEsRuleScript(esRuleScript));
    }

    @Log(title = "脚本信息", businessType = BusinessType.INSERT)
    @PostMapping("/addAll")
    public AjaxResult addAll(@RequestBody List<EsRuleScript> esRuleScriptList, @RequestParam Long sceneId)
    {
//        if (sceneId != null){
//            EsRuleScript esRuleScript = new EsRuleScript();
//            esRuleScript.setSceneId(sceneId);
//            List<EsRuleScript> list = esScriptService.selectEsRuleScriptList(esRuleScript);
//            Long[] ids = new Long[list.size()];
//            for (int i = 0; i < list.size(); i++){
//                ids[i] = list.get(i).getId();
//            }
//
//            if (ids.length > 0){
//                esScriptService.deleteEsRuleScriptByIds(ids);
//            }
//
//        }

        for (EsRuleScript esRuleScript : esRuleScriptList){
            esRuleScript.setContent(esRuleScript.getContent().replaceAll("&lt;", "<").replaceAll("&gt;", ">"));
            if (esRuleScript.getId() != null){
                esScriptService.updateEsRuleScript(esRuleScript);
            }else {
                esScriptService.insertEsRuleScript(esRuleScript);
            }

        }

        return toAjax(true);
    }

    /**
     * 修改脚本信息
     */
//    @PreAuthorize(hasPermi = "edsta:generation:edit")
    @Log(title = "脚本信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EsRuleScript esRuleScript)
    {
        return toAjax(esScriptService.updateEsRuleScript(esRuleScript));
    }

    /**
     * 删除脚本信息
     */
//    @PreAuthorize(hasPermi = "edsta:generation:remove")
    @Log(title = "脚本信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(esScriptService.deleteEsRuleScriptByIds(ids));
    }
}
